<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use \think\Session;
	
//<!--——————————马兴欣		商品管理——————————-->
class Goods extends Controller 
{
    public function index()
    {
    	$goods=Db::name("goods")->select();
        $this->assign("goods",$goods);   	
    	return $this->fetch();
    }
    public function add()
    { 	
    	return $this->fetch();
    }
    public function update() {
        
        
       	//上传
        //
        $info="";
        $file = request()->file('goods_pic');
        
        if($file){
            $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'.DS.'img'.DS.'goods');//DS为/
            echo(123);
            if (request()->isPost()) {
                $data=[
                'goods_name'=>input('param.goods_name'),
                'goods_intro'=>input('param.goods_intro'),
                'goods_status'=>"1",
                'goods_pic'=>$info->getFilename(),   	  
                'goods_value'=>input('param.goods_value'),
                'goods_addtime'=>date("Y-m-d H:i:s"),
                ];
            }
            //存入数据库	
            $res=Db::name("goods")-> insert($data);
            if($res){
                $this->success("添加成功",'goods/index');
            }else{
                $this->error("添加失败");
            }     
        }
    }
    /*——————————马兴欣		删除商品——————————*/
    public function delete($id){
        $res = Db::name("goods")->where("goods_id=$id")->find();
        $goods_pic = $res["goods_pic"];
        if(Db::name("goods")->delete($id)){
            unlink(ROOT_PATH . 'public' . DS . 'static'.DS.'img'.DS.'goods' .DS. $goods_pic);   
            $this->success("删除成功",'goods/index');
        }else{
            $this->error("删除失败");
        }
    } 	

    public function edit($id){
        $res = Db::name("goods")->where('goods_id',$id)->find();
        $this->assign("goods",$res);
        return $this->fetch();
    }

    public function goods_edit(){
        $goods_id = input("goods_id");
		$info="";
    	$file = request()->file('goods_pic');//手册里上传文件
    	if($file){
            $res = Db::name("goods")->where("goods_id=$goods_id")->find();
            $goods_pic = $res["goods_pic"];
            unlink(ROOT_PATH.'public'.DS.'static'.DS.'img'.DS. 'goods' .DS. $goods_pic);		
            $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'.DS.'img'.DS. 'goods');
             
            if(request()->isPOST()){
    //    		$typename=input('param.typename');
                $data=[
                    'goods_name'=>input('param.goods_name'),
                    'goods_intro'=>input('param.goods_intro'),
                    'goods_pic'=>$info->getFilename(),   	  
                    'goods_value'=>input('param.goods_value'),
                ];
            }
                //存入数据库;
            $res = Db::name("goods")->where("goods_id",$goods_id)->update($data);
            if($res){
                $this->success("修改商品信息成功",'goods/index');
                // echo "{\"success\":\"1\"}
            } else {
                $this->error("修改商品信息失败");
                dump($this);     
            }
        }
      	if(request()->isPOST()){
//    		$typename=input('param.typename');
			$data=[
                'goods_name'=>input('param.goods_name'),
                'goods_intro'=>input('param.goods_intro'),  
                'goods_value'=>input('param.goods_value'),
			];
        }
        $res = Db::name("goods")->where("goods_id",$goods_id)->update($data);
        if($res){
            $this->success("修改商品信息成功",'goods/index');
            // echo "{\"success\":\"1\"}
        }else{
        $this->error("修改商品信息失败");
            dump($this);     
        }	

    }
 
}
